package com.qf.day10;

import java.sql.Array;
import java.util.Arrays;

/**
 * 解析url
 * 写一个方法，返回url中的所有参数的参数名
 * 例如，对于 url = "http://www.baidu.com/s?tn=json&word=onepiece&rn=10"
 * 返回： [tn, word, rn]
 */
public class work1 {
    public static void main(String[] args) {
        String url = "https://www.baidu.com/s?tn=json&word=onepiece&rn=10";
        String[] nameArr = parseParamNames(url);
        System.out.println(Arrays.toString(nameArr));
    }

    public static String[] parseParamNames(String url) {
        if(url == null){
            return null;
        }
        String[] split = url.split("\\?");
        String[] result = new String[1];
        int count=0;
        if(split.length>=2){
            String arr = split[1];
            String[] split1 = arr.split("&");
            for (String s : split1) {
                String[] split2 = s.split("=");
                for (int i = 0; i < split2.length; i+=2) {
                    if(count == result.length){
                        result = Arrays.copyOf(result,result.length+1);
                    }
                    result[count++] = split2[i];
                }
            }
        }else {
            return null;
        }
        return result;
    }
}
